home *** CD-ROM | disk | FTP | other *** search
/ Chip 2001 June / CHIP Haziran 2001.iso / prog / haziran / 19 / setup.exe / data.z / usermode.c < prev    next >
C/C++ Source or Header  |  2001-04-11  |  6KB  |  158 lines

  1. ////////////////////////////////////////////////////////////////
  2. // File - USERMODE.C
  3. //
  4. // This is a sample for opening a kernel plugin, and performing a
  5. // function call to it.
  6. // 
  7. ////////////////////////////////////////////////////////////////
  8.  
  9. #include <stdio.h>
  10. #include "../../../include/windrvr.h"
  11. #include "../../../include/windrvr_int_thread.h"
  12. #include "../kptest_com.h"
  13.  
  14. static char line[256];
  15.  
  16. void DoKernelPlugInCall(HANDLE hWD, DWORD hKernelPlugIn)
  17. {
  18.     WD_KERNEL_PLUGIN_CALL kpCall;
  19.     KPTEST_VERSION kptestVer;
  20.  
  21.     BZERO (kpCall);
  22.     kpCall.hKernelPlugIn = hKernelPlugIn;
  23.     kpCall.dwMessage = KPTEST_MSG_VERSION;
  24.     kpCall.pData = &kptestVer;
  25.     // this will call KP_Call() callback in the kernel
  26.     WD_KernelPlugInCall(hWD, &kpCall);
  27.     printf ("KPTest version: %s\n",kptestVer.cVer);
  28. }
  29.  
  30. WD_INTERRUPT Intrp;
  31.  
  32. // KP_IntAtIrql() callback in the kernel will be called when an interrupt
  33. // occurs.
  34. // if KP_IntAtIrql() returns TRUE, then KP_IntAtDpc() callback in the kernel
  35. // will be called.
  36. // if KP_IntAtDpc() returns a value of 1 or greater, then interrupt_handler(),
  37. // that is waiting on WD_IntWait() will be called.
  38. VOID interrupt_handler (PVOID pData)
  39. {
  40.     printf ("Got interrupt %d\n", Intrp.dwCounter);
  41. }
  42.  
  43. void DoKernelPlugInInterrupt(HANDLE hWD, DWORD hKernelPlugIn)
  44. {
  45.     WD_CARD_REGISTER cardReg;
  46.     HANDLE thread_handle;
  47.  
  48.     BZERO(cardReg);
  49.     printf ("Enter the interrupt number to install (X to cancel):");
  50.     fgets (line, sizeof(line), stdin);
  51.     sscanf(line, "%d", &cardReg.Card.Item[0].I.Int.dwInterrupt);
  52.     if (toupper(line[0])=='X' || cardReg.Card.Item[0].I.Int.dwInterrupt==0)
  53.     {
  54.         printf ("install interrupt canceled\n");
  55.         return;
  56.     }
  57.     cardReg.Card.Item[0].item = ITEM_INTERRUPT;
  58.     cardReg.Card.dwItems = 1;
  59.     WD_CardRegister(hWD, &cardReg);
  60.     if (!cardReg.hCard)
  61.     {
  62.         printf ("Failed installing interrupt\n");
  63.         return;
  64.     }
  65.  
  66.     BZERO(Intrp);
  67.     Intrp.hInterrupt = cardReg.Card.Item[0].I.Int.hInterrupt;
  68.     // tell WinDriver that this interrupt will be handled by a 
  69.     // Kernel PlugIn.
  70.     Intrp.kpCall.hKernelPlugIn = hKernelPlugIn;
  71.     // this calls WD_IntEnable() and creates an interrupt handler thread
  72.     // also it call KP_IntEnable() callback in the kernel
  73.     if (!InterruptThreadEnable(&thread_handle, hWD, &Intrp, interrupt_handler, NULL))
  74.     {
  75.         printf ("failed enabling interrupt\n");
  76.     }
  77.     else
  78.     {
  79.         // call your driver code here 
  80.         printf ("Press Enter to uninstall interrupt\n");
  81.         fgets(line, sizeof(line), stdin);
  82.         
  83.         // this calls WD_IntDisable()
  84.         // also it call KP_IntDisable() callback in the kernel
  85.         InterruptThreadDisable(thread_handle);
  86.     }
  87.     WD_CardUnregister(hWD, &cardReg);
  88. }
  89.  
  90. int main( int argc, char *argv[] )
  91. {
  92.     HANDLE hWD = INVALID_HANDLE_VALUE;
  93.     WD_VERSION ver;
  94.     WD_KERNEL_PLUGIN kernelPlugIn;
  95.  
  96.     hWD = WD_Open();
  97.     if (hWD==INVALID_HANDLE_VALUE)
  98.     {
  99.         printf ("error opening WINDRVR\n");
  100.         return EXIT_FAILURE;
  101.     }
  102.  
  103.     BZERO(ver);
  104.     WD_Version (hWD, &ver);
  105.     printf (WD_PROD_NAME " version - %s\n", ver.cVer);
  106.     if (ver.dwVer<WD_VER)
  107.     {
  108.         printf ("error incorrect WINDRVR version. needs ver %d\n",WD_VER);
  109.         WD_Close(hWD);
  110.         return EXIT_FAILURE;
  111.     }
  112.  
  113.     BZERO (kernelPlugIn);
  114.     kernelPlugIn.pcDriverName = "KPTEST"; // this will search for KPTEST.VXD or KPTEST.SYS
  115.                                           // VXD: for Windows 95/98/ME: under \WINDOWS\SYSTEM\VMM32 
  116.                                           // SYS: for Windows NT/2K: under \WINDOWS\SYSTEM32\DRIVERS 
  117.  
  118.     // If you need to load the driver from an absolute path, instead of the above paths, you
  119.     // should specify as follows:
  120.     // for VXD under 95/98/ME
  121.     // kernelPlugIn.pcDriverPath = "C:\\WINDRVR\\KERPLUG\\KPTEST\\KERMODE\\WIN95\\KPTEST.VXD";
  122.     // for SYS under NT/2K
  123.     // kernelPlugIn.pcDriverPath = "C:\\WINDRVR\\KERPLUG\\KPTEST\\KERMODE\\WINNT\\KPTEST.SYS";
  124.     //
  125.     // NOTE: 
  126.     //   The Windows 98 kernel does not accept long file names. So make sure that the path
  127.     //   specified above does not contain any file or directory name that does not follow
  128.     //   the MSDOS 8.3 filename limitation. For example "WINDRIVER" is not acceptable. 
  129.     //   If you indeed have a name longer than 8 characters, then please find its legacy
  130.     //   MSDOS name. For example, WINDRIVER would probably map to WINDRV~1. You can use
  131.     //   the Windows 98 MSDOS command prompt to find this - the DIR listing will show
  132.     //   both the long name as well as the MSDOS name
  133.  
  134.     // this will call KP_Open() callback in the kernel
  135.     WD_KernelPlugInOpen(hWD, &kernelPlugIn);            
  136.     if (!kernelPlugIn.hKernelPlugIn)
  137.     {
  138.         printf ("There was an error loading driver %s\n",kernelPlugIn.pcDriverName);
  139.         printf ("\n");
  140.         printf ("If you are using Win95/98:\n");
  141.         printf ("  Make sure you copied KPTEST.VXD to c:\\win95\\system\\vmm32.\n");
  142.         printf ("\n");
  143.         printf ("If you are using WinNT:\n");
  144.         printf ("  Make sure you copied KPTEST.SYS to c:\\winnt\\system32\\drivers.\n");
  145.         printf ("  Run: \\windriver\\util\\WDREG -name KPTEST install\n");
  146.         return EXIT_FAILURE;
  147.     }
  148.     printf("Kernel Plugin opened\n");
  149.  
  150.     DoKernelPlugInCall(hWD, kernelPlugIn.hKernelPlugIn);
  151.     DoKernelPlugInInterrupt(hWD, kernelPlugIn.hKernelPlugIn);
  152.  
  153.     // this will call KP_Close() callback in the kernel
  154.     WD_KernelPlugInClose(hWD, &kernelPlugIn);
  155.     WD_Close(hWD);
  156.     return EXIT_SUCCESS;
  157. }
  158.